<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class kst_penerimaan_return_toko extends MY_Controller {
	
	function __construct()
	{
		parent::__construct(); 
		
		$this->load->model('kst_penerimaan_return_toko_model', 'Tagihan');
		$this->load->model('kst_dt_penerimaan_return_toko_model', 'Detail');
		$this->load->model('combo_model', 'Combo');
		$this->secure_session();
	}
	
	function index(){
		$data['title'] = "Data Order Receive";
		viewByTemplate('kst_penerimaan_return_toko/index', $data, 'grid');
	}
	 
	function getdata()
	{
		$this->secure_session();
		echo $this->Tagihan->GetJSON();
	}

	function dtl_sample2_data()
	{
		// Ajax Response For Datagrid
		echo $this->Detail->GetJSON(post('no_detail_tagihan'));
	}

	function create()
	{
		// Display Form Create
		$data['title'] = "Penerimaan Baru";
		$data['data'] = NULL;
		//$data['arr_reference']	= $this->Combo->SampleReference();
		$data['mode'] = "create";
		viewByTemplate('kst_penerimaan_return_toko/form', $data, 'form');
	}

	function edit()
	{
		// Display Form Edit
		$data['title'] = "Ubah Data Return";
		$data['data'] = $this->Tagihan->GetSingleView(get('no_tagihan_listrik'));
		
		// Define Combo Data
		$data['mode'] = "edit";
		viewByTemplate('kst_penerimaan_return_toko/form', $data, 'form');
	}
	
	function view()
	{
		// Display Form View
		$data['title'] = "View Data Oder Receive";
		$data['data'] = $this->Tagihan->GetSingleView(get('no_receive'));
		
		$data['mode'] = "view";
		viewByTemplate('kst_penerimaan_return_toko/form', $data, 'form');
	}

	private function validation()
	{
		$error = new ErrorList();

		// Get Error Template
		$tpl_empty = $this->MessageTemplate("ER001");
		$tpl_duplicate = $this->MessageTemplate("ER002");
		// Validation
		
		if (post("no_receive") == '') {
			$error->Add("no_receive", "No Tagihan Listrik", $tpl_empty);
		} else if (post("mode") == "create" && intval($this->get_data("SELECT 1 FROM kst_order_receive2 WHERE no_receive = '".@ereg_replace("[^a-zA-Z0-9_-]", "",@strtoupper(@trim(post("no_receive"))))."'")) > 0) {
			$error->Add("no_receive", "No Tagihan Listrik", $tpl_duplicate);
		}else if (post("petugas") == '') {
			$error->Add("petugas", "Petugas", $tpl_empty);
		}else if (post("no_tenant") == '') {
			$error->Add("no_tenant", "No Tenant", $tpl_empty);
		}

		return $error;
	}
	
	function ajax_post()
	{
		$is_error = 0;
		$ls_response = "";
		$ls_genid = "";

		$error = $this->validation();
		try
		{
			if (count( $error->Get() ) > 0) { $is_error = 1; }
			else
			{
				// Fill Post Data
				$data	= post_by_key(array('no_receive',  'no_permintaan', 'petugas', 'status_toko', 'tanggal'));
				$data['no_receive']	= @ereg_replace("[^a-zA-Z0-9_-]", "",@strtoupper(@trim($data['no_receive'])));
				
				if (post("mode") == "create")
				{
					$this->Tagihan->Add($data);

					$ls_response = "Data Tersimpan";

				}
				else if (post("mode") == "edit")
				{
					
					$this->Tagihan->Update($data, array('no_tagihan_listrik'	=> $data['no_tagihan_listrik']));

					$ls_response = "Data Tersimpan [edited]";
				}
			}


			
			// ----- >> Detail Sample 2
				$dtl_sample2	= $this->data['dtl_sample2'];
				$total_deleted	= $dtl_sample2['num_row']['deleted'];
				$total_inserted	= $dtl_sample2['num_row']['inserted'];
				$total_updated	= $dtl_sample2['num_row']['updated'];
				
				for($i=0,$max_i=$total_deleted; $i<$max_i; $i++){
					$row = $dtl_sample2['deleted'][$i];					
					$this->Detail->DeleteSingle($row['no_detail_tagihan']);
				}
				
				
				for($i=0,$max_i=$total_inserted; $i<$max_i; $i++){
					
					// Auto number kode detail penjualan
					
					
					$query = $this->db->query("SELECT no_detail_tagihan FROM (SELECT * FROM pro_dt_tagihan_sewa ORDER BY no_detail_tagihan DESC) WHERE rownum <= 1");
					$getquery = $query->row_array();
					$rowcount = $getquery['no_detail_tagihan'];
					$autonum = $rowcount + 1;

					$row = $dtl_sample2['inserted'][$i];
					$row['no_detail_tagihan'] = $autonum;
					$row['tanggal_pembayaran'] = $row['tanggal_pembayaran'];
					$row['nama_tenant'] = $row['nama_tenant'];
					$row['no_tenant'] = $row['no_tenant'];
					$row['biaya_tagihan'] = $row['biaya_tagihan'];
					$row['keterangan'] = $row['keterangan'];
					$this->Detail->Add($row);
				}
				
				for($i=0,$max_i=$total_updated; $i<$max_i; $i++){
					$row = $dtl_sample2['updated'][$i];					
					$kode_desain	= $row['no_detail_tagihan'];
					unset($row['no_detail_tagihan']);					
					//$row['jumlah'] = (float)$row['jumlah'];					
					$this->Detail->Update($row, array('no_detail_tagihan'	=> $no_detail_tagihan));
				}
			
		} catch(Exception $e) {
			$is_error = 1; $ls_response = $e->getMessage();
		}
		
		echo $this->PostResponse(json_encode(array(	'is_error'	=> $is_error,
													'fields'	=> $error->GetDictionary(),
													'msg'		=> $ls_response,
													'genid'		=> $ls_genid,
													'form_sender'=> post("form_sender"))
								));
	}




	function ajax_delete()
	{
		$ls_result	= "";
		$no_tagihan_listrik	= get('no_tagihan_listrik');
		
		if ($no_tagihan_listrik != "")
		{
			// Delete Single
			$arr_rule	= array('no_tagihan_listrik' => $no_tagihan_listrik);
			$ls_result = $this->Tagihan->DeleteSingle($arr_rule);

		}
		else
		{
			
			$num_row = intval($this->data['num_row']['deleted']);
			
			for ($i = 0; $i < $num_row; $i++)
			{
				$row	= $this->data['deleted'][$i];
				
				$arr_rule	= array('no_tagihan_listrik' => $row['no_tagihan_listrik']);
				$ls_result .= $this->Tagihan->DeleteSingle($arr_rule);
			}
		}
		
		echo $ls_result;
	}
}
?>